package day13;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//可缓存线程池
public class Demo {
    public static void main(String[] args) {
        ExecutorService es2 = Executors.newCachedThreadPool();
        for (int i = 0; i < 10000; i++) {
            es2.execute(()->{
                System.out.println(Thread.currentThread().getName());
            });
        }
        es2.shutdown();
        String[] names = {"辣椒炒肉","鸡脚","小鸡炖蘑菇","甲鱼","黄鳝"};
        long[] times = {1000,3000,5000,5000,2000};
        ExecutorService es = Executors.newCachedThreadPool();
        long start = System.currentTimeMillis();
        for (int i = 0; i < names.length; i++) {
            int index =i;
            es.execute(()->{
                System.out.println("开始做："+names[index]);
                try {
                    Thread.sleep(times[index]);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("已经做好"+names[index]);
            });
        }
        es.shutdown();
        //结束的时间，一定是线程池关闭了，而且里面的任务也执行完了
        //用于判断线程池里面的任务，是否执行结束
        //没有结束返回false 否则true
        //es.isTerminated();
        while (true){
            if (es.isTerminated()){//判断任务走没走完
                long stop = System.currentTimeMillis();
                System.out.println("所有菜都炒完了："+(stop - start)+"ms");
                break;
            }
        }
    }
}
